package com.fireghost.jdt.mainActivity;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.fireghost.jdt.R;
import com.fireghost.jdt.database.JobDbEntry;
import com.fireghost.jdt.database.JournalDeTravailDbAdapter;
import com.fireghost.jdt.database.TypeDbEntry;

public class JournalDeTravailActivity extends Activity {
    
	private ListView oLstDailyJobs;
	private ListViewDailyJobsAdapter oLstDailyJobsAdapter;
	private CalendarView oCalDayPicker;
	private JournalDeTravailDbAdapter oJdtDb;
	private ImageButton oBtnAddNewJob;
	private JobContentViewAdapter oJobContentViewAdapter;
	
	
	
	///////    Listeners creation
	/*
	 * Listener on CalendarView day change
	 */
	private OnDateChangeListener oCalDayPickerDateChangeListener = new OnDateChangeListener() {
		public void onSelectedDayChange(CalendarView view, int year, int month,
				int dayOfMonth) {
			long iTimestampDayAtMidnight = new GregorianCalendar(year, month + 1, dayOfMonth).getTimeInMillis();
			
	    	oLstDailyJobsAdapter.populate(iTimestampDayAtMidnight, oJdtDb);
		}
	};
	
	/*
	 * Listener on ListViewDailyJobs click
	 */
	private OnItemClickListener oListViewDailyJobsClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
			JobDbEntry oJobEntry = oJdtDb.getJobByID(oLstDailyJobsAdapter.getItemId(position));
			
			TypeDbEntry oType = oJdtDb.getTypeById(oJobEntry.getTypeID());
			if (oType.getTypeID() == -1) {
				oJobContentViewAdapter.populate(oJobEntry);
			}
			else {
				oJobContentViewAdapter.populate(oJobEntry, oType);
			}
			
			oJobContentViewAdapter.toViewMode();
		}
	};
	
	/*
	 * Listener on BtnAddNewJob click
	 */
	private OnClickListener oBtnAddNewJobClickListener = new OnClickListener() {
		public void onClick(View v) {
			
			// Make +1 month to the date returned by oCalDayPicker.getDate() (because he have 1 month less ...)
			GregorianCalendar oCalTemp = new GregorianCalendar();
			oCalTemp.setTimeInMillis(oCalDayPicker.getDate());
			oCalTemp.set(GregorianCalendar.MONTH, oCalTemp.get(GregorianCalendar.MONTH) + 1);
			long iTimestampTemp = oCalTemp.getTimeInMillis();
			
			JobDbEntry oNewJob = new JobDbEntry(
					getString(R.string.lang_new_job_description),
					iTimestampTemp
			);
			oNewJob.setJobID(oJdtDb.insertNewJob(oNewJob));
			
			oJobContentViewAdapter.populate(oNewJob);
			oJobContentViewAdapter.toEditMode();
			
			oLstDailyJobsAdapter.populate(
					new GregorianCalendar(
						oCalTemp.get(GregorianCalendar.YEAR),
						oCalTemp.get(GregorianCalendar.MONTH),
						oCalTemp.get(GregorianCalendar.DAY_OF_MONTH))
					.getTimeInMillis(),
					oJdtDb);
		}
	};
	
	
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jdt_main_view);
        
        /******************  oLstDailyEntries  ********************/
        // Init
        oJdtDb = new JournalDeTravailDbAdapter(this);
        oJdtDb.open();
        
        
        /******************  oLstDailyEntries  ********************/
        // Find the object in layout
        oLstDailyJobs = (ListView)findViewById(R.id.oLstDailyJobs);
        // Set listener
        oLstDailyJobs.setOnItemClickListener(oListViewDailyJobsClickListener);
        // Set adapter
        oLstDailyJobsAdapter = new ListViewDailyJobsAdapter(this);
        oLstDailyJobs.setAdapter(oLstDailyJobsAdapter);
        
        
        /******************  oCalDayPicker  ********************/
        // Find the object in layout
        oCalDayPicker = (CalendarView)findViewById(R.id.oCalDayPicker);
        // Set listener
        oCalDayPicker.setOnDateChangeListener(oCalDayPickerDateChangeListener);
        
        
        /******************  oBtnAddNewJob  ********************/
        // Find the object in layout
        oBtnAddNewJob = (ImageButton)findViewById(R.id.oBtnNewJob);
        // Set the listener
        oBtnAddNewJob.setOnClickListener(oBtnAddNewJobClickListener);
        
        
        /******************  oJobContentViewAdapter  ********************/
        // Init
        oJobContentViewAdapter = new JobContentViewAdapter(this, oJdtDb);
    }
    
    public void onPause() {
    	super.onPause();
    	oJdtDb.close();
    }
    
    public void onResume() {
    	super.onResume();
    	oJdtDb.open();
    }
}